package com.zdp.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.Jedis;

/**
 * @author sesshomaru
 * @date 2020/1/13 0:14
 */
@Configuration
//pom中有jedis的引入(有jedis这个类才会被创建)
@ConditionalOnClass(MyRedisTemplate.class)
@EnableConfigurationProperties(RedisProperties.class) //开启使用映射实体对象
@ConditionalOnProperty//存在对应配置信息时初始化该配置类
        (
                prefix = "zdp.redis",//存在配置前缀redis
                //value = "enabled",//开启
                //matchIfMissing = true//缺失检查
                value = "enabled", havingValue = "true"
        )
public class RedisAutoConfiguration {

    @Autowired
    private RedisProperties redisProperties;

    @Bean
    //当spring的环境没有名为jedis的bean时这个bean才会被注入
    @ConditionalOnMissingBean(MyRedisTemplate.class)
    public MyRedisTemplate myRedisTemplate(){
        return new MyRedisTemplate(redisProperties.getHost(),redisProperties.getPort());
    }
}
